﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.Collections;
using System.Collections.Generic;

namespace ActivityTracker.Library
{
    public class Schedules
    {
        #region Global

        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        IsolatedStorageFileStream isoStream;
        XDocument xdoc;


        #endregion

        #region Methods

        public void DeleteSchedulesBasedOnActId(string actId)
        {
            try
            {
                if (isoStore.FileExists("ActivityTracker/Schedules.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var eles = from t in xdoc.Descendants("schedule") where t.Element("actId").Value == actId select t;
                    foreach (var el in eles)
                        DeleteSchedule(el.Attribute("id").Value);
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in DeleteSchedulesBasedOnActId Method In ActivityTracker.Library.Schedules.Schedules file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("actId_DeleteSchedulesBasedOnActId", actId);
                foreach (DictionaryEntry d in ex.Data)
                    exNew.Data.Add(d.Key, d.Value);
                throw exNew;
            }
        }

        public void DeleteSchedule(string schedId)
        {
            try
            {
                isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Open, isoStore);
                xdoc = XDocument.Load(isoStream);
                isoStream.Close();
                (from t in xdoc.Descendants("schedule") where t.Attribute("id").Value == schedId select t).Remove();
                isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Create, isoStore);
                xdoc.Save(isoStream);
                isoStream.Close();
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in DeleteSchedule Method In ActivityTracker.Library.Schedules.Schedules file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("schedId_DeleteSchedule", schedId);
                throw exNew;
            }
        }

        public void SaveNewSchedule(DateTime schedCreatedDate, DateTime schedCreatedTime, DateTime schedEndDate, DateTime schedEndTime, string schedType, string actId, string actName, string schedViewOrder)
        {
            try
            {
                DateTime startScheduleDate = schedCreatedDate;
                DateTime nextScheduleDate = schedCreatedDate;
                DateTime prevScheduleDate = schedCreatedDate;

                switch (schedType)
                {
                    case "Daily":
                        nextScheduleDate = schedCreatedDate.AddDays(1);
                        break;
                    case "Weekly":
                        nextScheduleDate = schedCreatedDate.AddDays(7);
                        break;
                    case "Weekdays":
                        if (schedCreatedDate.DayOfWeek.ToString() == "Saturday")
                        {
                            startScheduleDate = schedCreatedDate.AddDays(2);
                            nextScheduleDate = startScheduleDate.AddDays(1);
                        }
                        else if (schedCreatedDate.DayOfWeek.ToString() == "Sunday")
                        {
                            startScheduleDate = schedCreatedDate.AddDays(1);
                            nextScheduleDate = startScheduleDate.AddDays(1);
                        }
                        else
                        {
                            nextScheduleDate = schedCreatedDate.AddDays(1);
                        }
                        break;
                    case "Weekends":
                        if (schedCreatedDate.DayOfWeek.ToString() == "Sunday")
                        {
                            startScheduleDate = schedCreatedDate;
                            nextScheduleDate = startScheduleDate.AddDays(6);
                        }
                        if (schedCreatedDate.DayOfWeek.ToString() == "Monday")
                        {
                            startScheduleDate = schedCreatedDate.AddDays(5);
                            nextScheduleDate = startScheduleDate.AddDays(1);
                        }
                        if (schedCreatedDate.DayOfWeek.ToString() == "Tuesday")
                        {
                            startScheduleDate = schedCreatedDate.AddDays(4);
                            nextScheduleDate = startScheduleDate.AddDays(1);
                        }
                        if (schedCreatedDate.DayOfWeek.ToString() == "Wednesday")
                        {
                            startScheduleDate = schedCreatedDate.AddDays(3);
                            nextScheduleDate = startScheduleDate.AddDays(1);
                        }
                        if (schedCreatedDate.DayOfWeek.ToString() == "Thursday")
                        {
                            startScheduleDate = schedCreatedDate.AddDays(2);
                            nextScheduleDate = startScheduleDate.AddDays(1);
                        }
                        if (schedCreatedDate.DayOfWeek.ToString() == "Friday")
                        {
                            startScheduleDate = schedCreatedDate.AddDays(1);
                            nextScheduleDate = startScheduleDate.AddDays(1);
                        }
                        if (schedCreatedDate.DayOfWeek.ToString() == "Saturday")
                        {
                            startScheduleDate = schedCreatedDate;
                            nextScheduleDate = startScheduleDate.AddDays(1);
                        }
                        break;
                    case "Alternate Days":
                        nextScheduleDate = schedCreatedDate.AddDays(2);
                        break;
                    case "Monthly":
                        nextScheduleDate = schedCreatedDate.AddMonths(1);
                        break;
                    case "Yearly":
                        nextScheduleDate = schedCreatedDate.AddYears(1);
                        break;
                }
                SaveSchedule(actId, actName, schedType, schedCreatedDate.ToShortDateString(), schedCreatedTime.ToShortTimeString(), schedEndDate.ToShortDateString(), schedEndTime.ToShortTimeString(), prevScheduleDate.ToShortDateString(), startScheduleDate.ToShortDateString(), nextScheduleDate.ToShortDateString(), schedViewOrder);
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in SaveSchedule Method In ActivityTracker.Library.Schedules.Schedules file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                foreach (DictionaryEntry d in ex.Data)
                {
                    exNew.Data.Add(d.Key, d.Value);
                }
                throw exNew;
            }
        }

        public void SaveSchedule(string actId, string actName, string schedType, string schedCreatedDate, string schedCreatedTime, string schedEndDate, string schedEndTime, string prevScheduleDate, string startScheduleDate, string nextScheduleDate, string schedViewOrder)
        {
            try
            {
                Actvity objActivity = new Actvity();
                string imagename = objActivity.GetActivityImageName(actId);
                if (!isoStore.FileExists("ActivityTracker/Schedules.xml"))
                {
                    int id = 1;
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Create, isoStore);
                    xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"),
                    new XElement("schedules",
                    new XElement("schedule", new XAttribute("id", id), new XAttribute("schedType", schedType),
                    new XElement("actId", actId),
                    new XElement("actName", actName),
                    new XElement("actImage", imagename),
                    new XElement("schedCreatedDate", schedCreatedDate),
                    new XElement("schedCreatedTime", schedCreatedTime),
                    new XElement("schedEnddate", schedEndDate),
                    new XElement("schedEndTime", schedEndTime),
                    new XElement("prevSchedDate", prevScheduleDate),
                    new XElement("startSchedDate", startScheduleDate),
                    new XElement("nextSchedDate", nextScheduleDate),
                    new XElement("viewcount", 0),
                    new XElement("schedViewOrder", schedViewOrder))));
                    xdoc.Save(isoStream);
                    isoStream.Close();
                }
                else
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Open, FileAccess.Read, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    int NextId = 1;
                    var schedules = from c in xdoc.Descendants("schedule") orderby (int)c.Attribute("id") descending select c;
                    foreach (var sched in schedules)
                    {
                        NextId = int.Parse(sched.Attribute("id").Value) + 1;
                        break;
                    }
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Create, isoStore);
                    xdoc.Root.Add(new XElement("schedule", new XAttribute("id", NextId), new XAttribute("schedType", schedType),
                    new XElement("actId", actId),
                    new XElement("actName", actName),
                    new XElement("actImage", imagename),
                    new XElement("schedCreatedDate", schedCreatedDate),
                    new XElement("schedCreatedTime", schedCreatedTime),
                    new XElement("schedEnddate", schedEndDate),
                    new XElement("schedEndTime", schedEndTime),
                    new XElement("prevSchedDate", prevScheduleDate),
                    new XElement("startSchedDate", startScheduleDate),
                    new XElement("nextSchedDate", nextScheduleDate),
                    new XElement("viewcount", 0),
                    new XElement("schedViewOrder", schedViewOrder)));
                    xdoc.Save(isoStream);
                    isoStream.Close();
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in SaveSchedule Method In ActivityTracker.Library.Schedules.Schedules file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("actId_SaveSchedule", actId);
                exNew.Data.Add("actName_SaveSchedule", actName);
                exNew.Data.Add("schedType_SaveSchedule", schedType);
                exNew.Data.Add("SchedCreatedDate_SaveSchedule", schedCreatedDate);
                exNew.Data.Add("SchedCreatedTime_SaveSchedule", schedCreatedTime);
                exNew.Data.Add("SchedEndDate_SaveSchedule", schedEndDate);
                exNew.Data.Add("SchedEndTime_SaveSchedule", schedEndTime);
                exNew.Data.Add("PreviousScheduleDate_SaveSchedule", prevScheduleDate);
                exNew.Data.Add("StartScheduleDate_SaveSchedule", startScheduleDate);
                exNew.Data.Add("NextScheduleDate_SaveSchedule", nextScheduleDate);
                exNew.Data.Add("SchedViewOrder_SaveSchedule", schedViewOrder);
                throw exNew;
            }
        }

        //LoadSchedulesOnCurrentDate
        public List<ActivityProp> LoadSchedsOfCurrentDate()
        {
            List<ActivityProp> SchedulesList = new List<ActivityProp>();
            try
            {
                if (isoStore.FileExists("ActivityTracker/Schedules.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var schedules = from t in xdoc.Descendants("schedule")
                                    where t.Element("startSchedDate").Value == DateTime.Today.ToShortDateString()
                                    orderby int.Parse(t.Element("schedViewOrder").Value) ascending, t.Element("schedCreatedTime").Value ascending
                                    select t;
                    foreach (var sched in schedules)
                    {
                        ActivityProp prop = new ActivityProp();
                        prop.SchedType = sched.Attribute("schedType").Value;
                        prop.ActId = sched.Element("actId").Value;
                        prop.ActName = sched.Element("actName").Value;
                        prop.SchedId = sched.Attribute("id").Value;
                        prop.SchedCreatedTime = sched.Element("schedCreatedTime").Value;
                        prop.SchedEndTime = sched.Element("schedEndTime").Value;
                        SchedulesList.Add(prop);
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in LoadSchedsOfCurrentDate Method In ActivityTracker.Library.Schedules.Schedules file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                throw exNew;
            }
            return SchedulesList;
        }

        public void UpdateCurrentSchedules()
        {
            try
            {
                if (isoStore.FileExists("ActivityTracker/Schedules.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();
                    var schedules = from t in xdoc.Descendants("schedule")
                                    where t.Element("nextSchedDate").Value == System.DateTime.Now.ToShortDateString()
                                    select t;
                    foreach (XElement scheds in schedules)
                    {
                        if (scheds.Element("startSchedDate") != null)
                        {
                            scheds.Element("prevSchedDate").Value = scheds.Element("startSchedDate").Value;
                            scheds.Element("startSchedDate").Value = scheds.Element("nextSchedDate").Value;
                            DateTime CurrentDate = Convert.ToDateTime(scheds.Element("nextSchedDate").Value);
                            DateTime NextDate = System.DateTime.Now;
                            switch (scheds.Attribute("schedType").Value)
                            {
                                case "Daily":
                                    NextDate = CurrentDate.AddDays(1);
                                    break;
                                case "Weekly":
                                    NextDate = CurrentDate.AddDays(7);
                                    break;
                                case "Weekdays":
                                    NextDate = CurrentDate.AddDays(1);
                                    if (CurrentDate.DayOfWeek.ToString() == "Saturday")
                                    {
                                        NextDate = CurrentDate.AddDays(2);
                                    }
                                    if (CurrentDate.DayOfWeek.ToString() == "Sunday")
                                    {
                                        NextDate = CurrentDate.AddDays(1);
                                    }
                                    break;
                                case "Weekends":
                                    if (CurrentDate.DayOfWeek.ToString() == "Sunday")
                                    {
                                        NextDate = CurrentDate.AddDays(6);
                                    }
                                    if (CurrentDate.DayOfWeek.ToString() == "Monday")
                                    {
                                        NextDate = CurrentDate.AddDays(5);
                                    }
                                    if (CurrentDate.DayOfWeek.ToString() == "Tuesday")
                                    {
                                        NextDate = CurrentDate.AddDays(4);
                                    }
                                    if (CurrentDate.DayOfWeek.ToString() == "Wednesday")
                                    {
                                        NextDate = CurrentDate.AddDays(3);
                                    }
                                    if (CurrentDate.DayOfWeek.ToString() == "Thursday")
                                    {
                                        NextDate = CurrentDate.AddDays(2);
                                    }
                                    if ((CurrentDate.DayOfWeek.ToString() == "Friday") || (CurrentDate.DayOfWeek.ToString() == "Saturday"))
                                    {
                                        NextDate = CurrentDate.AddDays(1);
                                    }
                                    break;
                                case "Alternate Days":
                                    NextDate = CurrentDate.AddDays(2);
                                    break;
                                case "Monthly":
                                    NextDate = CurrentDate.AddMonths(1);
                                    break;
                                case "Yearly":
                                    NextDate = CurrentDate.AddYears(1);
                                    break;
                            }
                            scheds.Element("nextSchedDate").Value = (string)NextDate.ToShortDateString();
                            isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Create, isoStore);
                            xdoc.Save(isoStream);
                            isoStream.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in UpdateCurrentSchedules Method In ActivityTracker.Library.Schedules.Schedules file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                throw exNew;
            }
        }

        public int NoOfSchedulesToDelete(string actId)
        {
            int count = 0;
            try
            {
                if (isoStore.FileExists("ActivityTracker/Schedules.xml"))
                {
                    isoStream = new IsolatedStorageFileStream("ActivityTracker/Schedules.xml", FileMode.Open, isoStore);
                    xdoc = XDocument.Load(isoStream);
                    isoStream.Close();

                    count = (from i in xdoc.Descendants("schedule") where i.Element("actId").Value == actId select i).Count();
                }
            }
            catch (Exception ex)
            {
                Exception exNew = new Exception("Exception in NoOfSchedulesToDelete Method In ActivityTracker.Library.Schedules.Schedules file\n\n" + ex.Message + " \n\n Stack Trace \n" + ex.StackTrace);
                exNew.Data.Add("actId", actId);
                throw exNew;
            }
            return count;
        }

        #endregion
    }
}
